package ar.edu.unq.tpi.uis.tepe3.persistencia;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;

public abstract class GenericHibernateDAO<T, ID extends Serializable> {
	
	private Class<T> persistentClass;
	private Session session;
	
	@SuppressWarnings("unchecked")
	public GenericHibernateDAO() {
		this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		
	}
	
	public void setSession(Session s) {
        this.session = s;
    }
 
    public Session getSession() {
        if (session == null)
            throw new IllegalStateException("Session has not been set on DAO before usage");
        return session;
    }
 
    public Class<T> getPersistentClass() {
        return persistentClass;
    }
 
    @SuppressWarnings("unchecked")
    public T findById(ID id, boolean lock) {
    	this.getSession().beginTransaction();
        T entity;
        if (lock)
            entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
        else
            entity = (T) getSession().load(getPersistentClass(), id);
 
        return entity;
    }
 
    public List<T> findAll() {
        return findByCriteria();
    }
 
    @SuppressWarnings("unchecked")
//	public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
   	public List<T> findByExample(T exampleInstance) {
    	this.getSession().beginTransaction();
        Criteria crit = getSession().createCriteria(getPersistentClass());
        Example example =  Example.create(exampleInstance);
        /*for (String exclude : excludeProperty) {
            example.excludeProperty(exclude);
        }*/
        crit.add(example);
        return crit.list();
    }
 
    public T makePersistent(T entity) {
//    	this.getSession().beginTransaction();
        getSession().saveOrUpdate(entity);
        return entity;
    }
 
    public void makeTransient(T entity) {
    	this.getSession().beginTransaction();
        getSession().delete(entity);
    }
 
    public void flush() {
        getSession().flush();
//        this.getSession().getTransaction().commit();
    }
 
    public void clear() {
        getSession().clear();
    }
 
    /**
     * Use this inside subclasses as a convenience method.
     */
    @SuppressWarnings("unchecked")
    protected List<T> findByCriteria(Criterion... criterion) {
    	this.getSession().beginTransaction();
        Criteria crit = getSession().createCriteria(getPersistentClass());
        for (Criterion c : criterion) {
            crit.add(c);
        }
        return crit.list();
   }
}
